package com.znufe.exam.service.excel;

import com.alibaba.excel.util.StringUtils;
import com.znufe.exam.utility.easyexcel.ExcelCheckErrDto;
import com.znufe.exam.utility.easyexcel.ExcelCheckResult;
import com.znufe.exam.viewmodel.admin.question.excelmodel.MultipleChoiceFromExcelVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MultipleChoiceServiceImpl implements MultipleChoiceService {

    @Autowired
    public MultipleChoiceServiceImpl() {}

    @Override
    public ExcelCheckResult checkImportExcel(List<MultipleChoiceFromExcelVM> multipleChoiceList, Integer questionType) {
        //成功结果集
        List<MultipleChoiceFromExcelVM> successList = new ArrayList<>();
        //错误数组
        List<ExcelCheckErrDto<MultipleChoiceFromExcelVM>> errList = new ArrayList<>();

        for (MultipleChoiceFromExcelVM multipleChoice : multipleChoiceList) {
            // 错误信息
            StringBuilder errMsg = new StringBuilder();
            if (null == multipleChoice.getTitle()) {
                errMsg.append("多选题题干不能为空！请修改").append(";");
            }
            if (null == multipleChoice.getOptionA() || null == multipleChoice.getOptionB()) {
                errMsg.append("选项A或选项B的内容为空，请至少设置两个选项内容！").append(";");
            }
            System.out.println("A：" + multipleChoice.getChooseA());
            System.out.println("B: " + multipleChoice.getChooseB());
            System.out.println("C: " + multipleChoice.getChooseC());
            System.out.println("D: " + multipleChoice.getChooseD());
            System.out.println("E: " + multipleChoice.getChooseE());
            System.out.println("F：" + multipleChoice.getChooseF());
            if (null == multipleChoice.getChooseA() && null == multipleChoice.getChooseB() &&
                    null == multipleChoice.getChooseC() && null == multipleChoice.getChooseD() &&
                    null == multipleChoice.getChooseE() && null == multipleChoice.getChooseF()) {
                errMsg.append("答案不能为空！请为题目设置正确答案！").append(";");
            } else {
                int ansCount = 0;
                ansCount = "A".equals(multipleChoice.getChooseA()) ? ++ansCount : ansCount;
                ansCount = "B".equals(multipleChoice.getChooseB()) ? ++ansCount : ansCount;
                ansCount = "C".equals(multipleChoice.getChooseC()) ? ++ansCount : ansCount;
                ansCount = "D".equals(multipleChoice.getChooseD()) ? ++ansCount : ansCount;
                ansCount = "E".equals(multipleChoice.getChooseE()) ? ++ansCount : ansCount;
                ansCount = "F".equals(multipleChoice.getChooseF()) ? ++ansCount : ansCount;
                if (ansCount < 2) {
                    errMsg.append("多选题答案至少为两项！请重新设置题目答案！").append(";");
                } else {
                    if (multipleChoice.getChooseC() != null && multipleChoice.getOptionC() == null) {
                        errMsg.append("答案包含选项C，而选项C的内容为空，请重新设置！").append(";");
                    }
                    if (multipleChoice.getChooseD() != null && multipleChoice.getOptionD() == null) {
                        errMsg.append("答案包含选项D，而选项D的内容为空，请重新设置！").append(";");
                    }
                    if (multipleChoice.getChooseE() != null && multipleChoice.getOptionE() == null) {
                        errMsg.append("答案包含选项E，而选项E的内容为空，请重新设置！").append(";");
                    }
                    if (multipleChoice.getChooseF() != null && multipleChoice.getOptionF() == null) {
                        errMsg.append("答案包含选项F，而选项F的内容为空，请重新设置！").append(";");
                    }
                }
            }
            if (null == multipleChoice.getScore()) {
                errMsg.append("分数不能为空！请为题目设置分数！").append(";");
            }
            // 依据上面合法性判断的结果，
            // 正确的对象信息添加进successList，错误的对象信息添加进errList
            if (StringUtils.isEmpty(errMsg.toString())) {
                // 这里有两个选择，1、一个返回成功的对象信息，2、进行持久化操作
                System.out.println("SingleChoiceServiceImpl的 checkImportExcel里的 ：" + multipleChoice);
                successList.add(multipleChoice);
            } else {
                // 添加错误信息
                errList.add(new ExcelCheckErrDto(multipleChoice, errMsg.toString()));
            }
        }
        return new ExcelCheckResult(successList,errList);
    }
}
